﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using CB.Logic;
using Swift.Lib;
using Swift;
using System;

namespace CB.Client
{
    /// <summary>
    /// 拼图模式的游戏界面
    /// </summary>
    public class UIImageGame : UIGame<ImageGame, ImageBlock, ImageBlockIcon>
    {
        private void Awake()
        {
            base.Awake();

            var tbs = TargetBlocksTrans.GetComponentsInChildren<ImageBlockIcon>();
            int index = 0;
            for (int i = 0; i < 5; i++)
            {
                var l = new List<ImageBlockIcon>();
                for (int j = 0; j < 5; j++)
                {
                    l.Add(tbs[index]);
                    index++;
                }
                TargetBlockIcons.Add(l);
            }
        }

        private void Start()
        {
            FC.For2(gameData.Height, gameData.Width, (x, y) =>
            {
                var block = BlockIcons[y][x];
                block.InitMaterial();
            });
        }

        public override ImageGame GameData
        {
            get => gameData;
            protected set
            {
                if (value == null) return;

                gameData = value;

                var texname = UIUtils.GetRandomGameImage(new SRandom(gameData.SrandSeed));
                var sprites = Resources.LoadAll<Sprite>($"UI/ImageGame/{texname}");

                // 拼图区域
                for (int i = 0; i < gameData.Height; i++)
                {
                    for (int j = 0; j < gameData.Width; j++)
                    {
                        var block = gameData.Blocks[i][j];
                        BlockIcons[i][j].SetData(block);
                        var sn = $"{ texname }_{ block.TY * gameData.Width + block.TX}";
                        foreach (var s in sprites)
                        {
                            if (s.name == sn)
                            {
                                BlockIcons[i][j].SetSprite(s);
                                break;
                            }
                        }
                    }
                }

                // 目标图区域
                for (int i = 0; i < gameData.Height; i++)
                {
                    for (int j = 0; j < gameData.Width; j++)
                    {
                        var sn = $"{ texname }_{ i * gameData.Width + j}";
                        foreach (var s in sprites)
                        {
                            if (s.name == sn)
                            {
                                TargetBlockIcons[i][j].SetSprite(s);
                                break;
                            }
                        }
                    }
                }
            }
        }
        private ImageGame gameData;

        public override void SetData()
        {
            GameData = Player.Instance.GameData as ImageGame;
        }

        protected override void ShowTip(Dbase db)
        {
            base.ShowTip(db);

            for (int i = 0; i < gameData.Height; i++)
            {
                for (int j = 0; j < gameData.Width; j++)
                {
                    BlockIcons[i][j].ShowImage();
                }
            }
        }
        
        public override IEnumerator Dissolve(Action act)
        {
            OnAni = true;
            yield return new WaitForSeconds(1f);
            FC.For2(gameData.Height, gameData.Width, (x, y) =>
            {
                var block = BlockIcons[y][x];
                block.StartDissolve(DissolveTime / 2);
            });
            yield return new WaitForSeconds(DissolveTime/2);
            act?.Invoke();
            yield return new WaitForSeconds(DissolveTime/2);

            OnAni = false;
        }
    }
}